package entities;

import view.ChessBoard;

import javax.swing.*;
import java.awt.*;

/***
 * Author: Mr.Zhao, Time: 2018/5/1/20:19
 * Description: 根据棋子当前坐标生成的可移动的坐标点
 */
public class AvailablePoint extends JLabel {
    // 点的直径，单位：像素
    private int diameter = 16;
    private Color pointColor = Color.GREEN;
    private Point position;
    private ChessBoard chessBoard;

    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(pointColor);
        g.fillOval(0, 0, diameter, diameter);
    }

    public AvailablePoint(Point point) {
        chessBoard = ChessBoard.getInstance();
        setPosition(point.x, point.y);
    }

    public AvailablePoint(int x, int y) {
        chessBoard = ChessBoard.getInstance();
        setPosition(x, y);
    }

    private void setPosition(int x, int y) {
        this.position = new Point(x, y);
        /* 将棋子放置到父布局的坐标位置处 */
        this.setBounds(chessBoard.getMargin() + x * chessBoard.getCellWidth() - diameter / 2,
                chessBoard.getMargin() + y * chessBoard.getCellHeight() - diameter / 2,
                diameter, diameter);
    }

    public Point getPosition() {
        return this.position;
    }


    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof AvailablePoint)) {
            return false;
        }
        return this.getPosition().equals(((AvailablePoint) obj).getPosition());
    }

    @Override
    public int hashCode() {
        return this.getPosition().y + this.getPosition().x;
    }
}
